package lv.solcraft.replication.server.dao;

import java.util.ArrayList;
import java.util.List;

import lv.solcraft.replication.server.bdb.Database;
import lv.solcraft.replication.server.bdb.Database.OperSiteStore;
import lv.solcraft.replication.server.model.OperState;

public class OperSiteDataAccessFactory {

    private final Database database;

    public OperSiteDataAccessFactory(Database database) {
        this.database = database;
    }

    public OperSiteDataAccess getInstance(Long siteId, OperState operState) {
        return new OperSiteDataAccess(database.getOperSiteStore(siteId, operState), database);
    }

    public OperSiteDataAccess newOpers(Long siteId) {
        return getInstance(siteId, OperState.NEW);
    }

    public OperSiteDataAccess errorOpers(Long siteId) {
        return getInstance(siteId, OperState.ERROR);
    }

    public OperSiteDataAccess commitedOpers(Long siteId) {
        return getInstance(siteId, OperState.COMMITED);
    }

    public OperSiteDataAccess ignoredOpers(Long siteId) {
        return getInstance(siteId, OperState.IGNORED);
    }

    public List<OperSiteDataAccess> getAll() {
        List<OperSiteDataAccess> da = new ArrayList<OperSiteDataAccess>();
        for (OperSiteStore store : database.getOperSiteStores()) {
            da.add(new OperSiteDataAccess(store, database));
        }
        return da;
    }

    public List<OperSiteDataAccess> getAll(long siteId) {
        List<OperSiteDataAccess> da = new ArrayList<OperSiteDataAccess>();
        for (OperState state : OperState.values()) {
            da.add(getInstance(siteId, state));
        }
        return da;
    }
}
